namespace DotNetNuke.Entities.Modules.Definitions
{
    using DotNetNuke.Common;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Services.Localization;
    using System;
    using System.IO;
    using System.Web;
    using System.Xml;

    public class ModuleDefinitionValidator : XmlValidatorBase
    {
        private string GetDnnSchemaPath(Stream xmlStream)
        {
            ModuleDefinitionVersion Version = this.GetModuleDefinitionVersion(xmlStream);
            string schemaPath = "";
            switch (((int) Version))
            {
                case 0:
                    throw new Exception(this.GetLocalizedString("EXCEPTION_LoadFailed"));

                case 2:
                    schemaPath = @"components\ResourceInstaller\ModuleDef_V2.xsd";
                    break;

                case 3:
                    schemaPath = @"components\ResourceInstaller\ModuleDef_V2Skin.xsd";
                    break;

                case 4:
                    schemaPath = @"components\ResourceInstaller\ModuleDef_V2Provider.xsd";
                    break;

                case 5:
                    schemaPath = @"components\ResourceInstaller\ModuleDef_V3.xsd";
                    break;

                case 6:
                    schemaPath = @"components\ResourceInstaller\ModuleDef_V31.xsd";
                    break;
            }
            return Path.Combine(Globals.ApplicationMapPath, schemaPath);
        }

        private string GetLocalizedString(string key)
        {
            PortalSettings objPortalSettings = (PortalSettings) HttpContext.Current.Items["PortalSettings"];
            if (objPortalSettings == null)
            {
                return key;
            }
            return DotNetNuke.Services.Localization.Localization.GetString(key, objPortalSettings);
        }

        public ModuleDefinitionVersion GetModuleDefinitionVersion(Stream xmlStream)
        {
            xmlStream.Seek(0L, SeekOrigin.Begin);
            XmlTextReader xmlReader = new XmlTextReader(xmlStream);
            xmlReader.MoveToContent();
            switch (xmlReader.LocalName.ToLower())
            {
                case "module":
                    return ModuleDefinitionVersion.V1;

                case "dotnetnuke":
                    switch (xmlReader.GetAttribute("type"))
                    {
                        case "Module":
                            ModuleDefinitionVersion GetModuleDefinitionVersion=ModuleDefinitionVersion.VUnknown;
                            switch (xmlReader.GetAttribute("version"))
                            {
                                case "2.0":
                                    return ModuleDefinitionVersion.V2;

                                case "3.0":
                                    return ModuleDefinitionVersion.V3;

                                case "3.1":
                                    return ModuleDefinitionVersion.V3_1;
                            }
                            return GetModuleDefinitionVersion;

                        case "SkinObject":
                            return ModuleDefinitionVersion.V2_Skin;

                        case "Provider":
                            return ModuleDefinitionVersion.V2_Provider;
                    }
                    return ModuleDefinitionVersion.VUnknown;
            }
            return ModuleDefinitionVersion.VUnknown;
        }

        public override bool Validate(Stream XmlStream)
        {
            this.SchemaSet.Add("", this.GetDnnSchemaPath(XmlStream));
            return base.Validate(XmlStream);
        }
    }
}

